package com.jjwxc.jwjskandriod.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.base.FFActivity;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.framework.widget.MyDialog;
import com.jjwxc.jwjskandriod.model.UserProfileRequest;
import com.jjwxc.jwjskandriod.widget.PopViewUtil;

public class SecurityActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_phone;
    private TextView tv_nickName;
    private TextView tv_userNum;

    private ConstraintLayout cl_nick;
    private ConstraintLayout cl_device;
    private ConstraintLayout cl_tuichu;
    private ConstraintLayout cl_password;

    @Override
    public int getContentViewId() {
        return R.layout.activity_security;
    }

    @Override
    public void findView() {
        setTitle("账号与安全");
        tv_nickName = findViewById(R.id.tv_nickName);
        tv_phone = findViewById(R.id.tv_phone);
        tv_userNum = findViewById(R.id.tv_userNum);
        cl_nick = findViewById(R.id.cl_nick);
        cl_device = findViewById(R.id.cl_device);
        cl_tuichu = findViewById(R.id.cl_tuichu);
        cl_password = findViewById(R.id.cl_password);
    }

    @Override
    public void setListener() {
        cl_nick.setOnClickListener(this);
        cl_device.setOnClickListener(this);
        cl_tuichu.setOnClickListener(this);
        cl_password.setOnClickListener(this);
    }

    @Override
    public void afterCreate() {
        post(Url.getUserProfile, null, UserProfileRequest.class, new FFNetWorkCallBack<UserProfileRequest>() {
            @Override
            public void onSuccess(UserProfileRequest response) {
                if (response.getCode() == 200) {
                    tv_nickName.setText("昵称：" + response.getData().getNickname());

                    //进行加密
                    String phone = response.getData().getMobile().substring(0,3)+"****"+response.getData().getMobile().substring(7,response.getData().getMobile().length());

                    tv_phone.setText(phone);
                    tv_userNum.setText(response.getData().getUserNumber());
                }


            }

            @Override
            public boolean onFail(UserProfileRequest response) {
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.cl_nick) {
            updataNickNamePop(v, SecurityActivity.this, tv_nickName.getText().toString());
        } else if (v.getId() == R.id.cl_device) {
            Intent intent = new Intent(SecurityActivity.this, DeviceActivity.class);
            startActivity(intent);
        }else if(v.getId() == R.id.cl_password){
            Intent intent = new Intent(SecurityActivity.this, ResetPasswordAcitivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.cl_tuichu) {
            new MyDialog(SecurityActivity.this, "你确定要退出登录吗？", "确定", "取消", new MyDialog.DialogClickListener() {
                @Override
                public void onOkClick(Dialog dialog) {
                    PreUtils.romoveKey("RefreshToken");
                    PreUtils.romoveKey("token");
                    PreUtils.romoveKey("Authorization");
                    PreUtils.romoveKey("phone");
                    PreUtils.romoveKey("tourist");
                    removeAllActivity();
                    Intent intent = new Intent(SecurityActivity.this, LoginActivity.class);
                    startActivity(intent);
                }

                @Override
                public void onNoClick(Dialog dialog) {

                }
            }).show();

        }
    }

    public void updataNickNamePop(View v, Activity context, String name) {
        View view = LayoutInflater.from(context).inflate(R.layout.security_nickname_pop, null, false);
        EditText et_views = view.findViewById(R.id.et_views);
        TextView tv_quxiao = view.findViewById(R.id.tv_quxiao);
        TextView tv_queding = view.findViewById(R.id.tv_queding);
        et_views.setHint(name);

        //1.构造一个PopupWindow，参数依次是加载的 View，宽高
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //设置加载动画
        popWindow.setAnimationStyle(R.style.comment_dialog);
        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题

        popWindow.setTouchable(true);
        popWindow.setOnDismissListener(() -> PopViewUtil.setBackgroundAlpha(1, context));

        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效
        //设置 popupWindow 显示的位置，参数依次是参照 View，x轴的偏移量，y轴的偏移量
        popWindow.showAtLocation(v, Gravity.CENTER, 0, 0);

        PopViewUtil.setBackgroundAlpha(0.3f, context);
        tv_quxiao.setOnClickListener(v1 -> popWindow.dismiss());
        tv_queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((FFActivity) context).post(Url.updateUserProfile, "", String.class, new FFNetWorkCallBack<String>() {
                    @Override
                    public void onSuccess(String response) {
                        if (response.contains("200")) {
                            tv_nickName.setText("昵称：" + et_views.getText().toString());
                            popWindow.dismiss();
                        }

                    }

                    @Override
                    public boolean onFail(String response) {
                        popWindow.dismiss();
                        return false;
                    }
                }, "{\"nickname\":\"" + et_views.getText().toString() + "\"}");
            }
        });
    }

}

